/**
 * 论坛
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/6
 */
var self = '';

document.addEventListener('plusready' , ready ,  false);

var index = {};
 
var uid = memberInfo = '';
 
function ready(){ 
	
	self = plus.webview.currentWebview(); 
	memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);
	
 	//加载数据
   	info(); 
   	
   	//发表新帖子
   	$('.add-post').on('click' , function(){
		var add_post = plus.webview.create('add-post.html', 'add-post', {}, {
			page : self.id,
			uid : memberInfo.aid,
			avatar : memberInfo.avatar,
			nickname : memberInfo.nickname
		});
		add_post.addEventListener('loaded', function (){
			add_post.show('pop-in');
		})	
   	})
 	
}

function info(type){  
	
	plus.nativeUI.showWaiting('加载中...'); 
	
	$.ajax({  
		  
		type:"post", 
		
		url: apiRoot + '/Home/Article/post/' , 
		
		data:{
			tid : 30 ,
			uid : memberInfo.aid
		}, 
		dataType : 'json',
		success : function(data){ 
			plus.nativeUI.closeWaiting();
			if(data.all_post != ''){ 
				index[0] = 2;
				var html = '';
				$.each(data.all_post, function(k , v) {
					html += '<div data-uid="'+v.uid+'" data-id="'+v.id+'" data-tid="'+v.tid+'" class="table-view-cell table-view background-w overflow-h bbs-list-1 margin-top-5 lists">';
					html += '<i class="mui-pull-left "><img src="'+getImgUrl(v.avatar)+'"/></i>';  
					html += '<div class="mui-pull-left margin-bottom-10"><li class="overflow-h">';
					html += '<span class="mui-pull-left nickname">'+(v.nickname ? v.nickname : '')+'</span><span class="mui-pull-right">';
					html += '<span class="iconfont color-c font-14">&#xe622;</span><span class="number'+v.id+'"> '+v.number+' </span></span></li>'; 
					html += '<li class="font-12 color-c"><span class="addtime">'+v.addtime+'</span> <span style="color: #FE7E08;margin: 0 1em;font-size: 12px;">悬赏：<span class="bounty">'+(v.bounty ? v.bounty : 0)+'</span> 积分</span></li></div><div class="clear-both">'; 
					html += '<span class="ellipsis-1 title">'+v.title+'</span><div class="content" style="display:none">'+v.body+'</div></div></div>';
					
//					html(0 , v.id , v.tid , v.avatar , v.nickname , v.addtime , v.title , v.body , v.number);
				});
				$('#item1mobile .mui-scroll').html(html)
			}
			
			if(data.hot_post != ''){ 
				index[1] = 2;
				var html = '';
				$.each(data.hot_post, function(k , v) {
					html += '<div data-uid="'+v.uid+'" data-id="'+v.id+'" data-tid="'+v.tid+'" class="table-view-cell table-view background-w overflow-h bbs-list-1 margin-top-5 lists">';
					html += '<i class="mui-pull-left "><img src="'+getImgUrl(v.avatar)+'"/></i>';  
					html += '<div class="mui-pull-left margin-bottom-10"><li class="overflow-h">';
					html += '<span class="mui-pull-left nickname">'+(v.nickname ? v.nickname : '')+'</span><span class="mui-pull-right">';
					html += '<span class="iconfont color-c font-14">&#xe622;</span><span class="number'+v.id+'"> '+v.number+' </span></span></li>';
					html += '<li class="font-12 color-c"><span class="addtime">'+v.addtime+'</span> <span style="color: #FE7E08;margin: 0 1em;font-size: 12px;">悬赏：<span class="bounty">'+(v.bounty ? v.bounty : 0)+'</span> 积分</span></li></div><div class="clear-both">'; 
					html += '<span class="ellipsis-1 title">'+v.title+'</span><div class="content" style="display:none">'+v.body+'</div></div></div>';
//					html(1 , v.id , v.tid , v.avatar , v.nickname , v.addtime , v.title , v.body , v.number);
				});
				$('#item2mobile .mui-scroll').html(html) 
			}
			if(data.my_post != ''){    
				index[2] = 2;
				var html = '';
				$.each(data.my_post, function(k , v) {
					html += '<div data-uid="'+v.uid+'" data-id="'+v.id+'" data-tid="'+v.tid+'" class="table-view-cell table-view background-w overflow-h bbs-list-1 margin-top-5 lists">';
					html += '<i class="mui-pull-left "><img src="'+getImgUrl(v.avatar)+'"/></i>';  
					html += '<div class="mui-pull-left margin-bottom-10"><li class="overflow-h">';
					html += '<span class="mui-pull-left nickname">'+(v.nickname ? v.nickname : '')+'</span><span class="mui-pull-right">';
					html += '<span class="iconfont color-c font-14">&#xe622;</span><span class="number'+v.id+'"> '+v.number+' </span></span></li>';
					html += '<li class="font-12 color-c"><span class="addtime">'+v.addtime+'</span> <span style="color: #FE7E08;margin: 0 1em;font-size: 12px;">悬赏：<span class="bounty">'+(v.bounty ? v.bounty : 0)+'</span> 积分</span></li></div><div class="clear-both">'; 
					html += '<span class="ellipsis-1 title">'+v.title+'</span><div class="content" style="display:none">'+v.body+'</div></div></div>';
//					html(2 , v.id , v.tid , v.avatar , v.nickname , v.addtime , v.title ,  v.body , v.number);
				});
				$('#item3mobile .mui-scroll').html(html)
			} 
			details();
		},  
		
		error : function(e){ 
			
			plus.nativeUI.closeWaiting();
			
			toast('连接超时,请检查网络');
			
		} 
	});
}

 
function loadMore(post_type){   
	plus.nativeUI.showWaiting('加载中...');
	$.ajax({  
		type:"post",
		url: apiRoot + '/Home/Article/loadMore/' , 
		data:{
			tid : 30 ,
			page : index[post_type],
			post_type : post_type,
			uid : memberInfo.aid  
		}, 
		dataType : 'json',
		success : function(data){ 
			plus.nativeUI.closeWaiting();  
			if(data != ''){   
				index[post_type]++;
				$.each(data, function(k , v) {
					html(post_type ,v.id , v.tid , v.avatar , v.nickname , v.addtime , v.title , v.body , v.number , v.bounty , v.uid);
				});
			}else{
				toast('没有更多的数据了');
			}
		},  
		
		error : function(e){ 
			console.log(JSON.stringify(e))
			plus.nativeUI.closeWaiting();
			
			toast('连接超时,请检查网络');
			
		} 
	});
 	
}

 
function html(index , id ,tid , avatar , nickname , addtime , title , body , number  , bounty , userid){
	var html = '<div data-uid="'+userid+'" data-id="'+id+'" data-tid="'+tid+'" class="table-view-cell table-view background-w overflow-h bbs-list-1 margin-top-5 lists">';
	html += '<i class="mui-pull-left "><img src="'+getImgUrl(avatar)+'"/></i>';  
	html += '<div class="mui-pull-left margin-bottom-10"><li class="overflow-h">';
	html += '<span class="mui-pull-left nickname">'+(nickname ? nickname : '')+'</span><span class="mui-pull-right">';
	html += '<span class="iconfont color-c font-14">&#xe622;</span><span class="number'+id+'"> '+number+' </span></span></li>';
	html += '<li class="font-12 color-c"><span class="addtime">'+addtime+'</span> <span style="color: #FE7E08;margin: 0 1em;font-size: 12px;">悬赏：<span class="bounty">'+(bounty ? bounty : 0)+'</span> 积分</span></li></div><div class="clear-both">'; 
	html += '<span class="ellipsis-1 title">'+title+'</span><div class="content" style="display:none">'+body+'</div></div></div>';
	index == 0 ? $('#item1mobile .mui-scroll').append(html) : (index == 1 ? $('#item2mobile .mui-scroll').append(html) : $('#item3mobile .mui-scroll').append(html));
	details();
}
 
function details(){ 
	$('.lists').unbind();
	$('.lists').on('tap' , function(){ 
		plus.nativeUI.showWaiting('跳转中...');
		var bbs_inf = plus.webview.create('bbs-inf.html', 'bbs-inf', {}, {
			page : self.id,
			avatar : $(this).find('img').attr('src'),
			nickname : $(this).find('.nickname').text(),
			title : $(this).find('.title').text(),
			addtime : $(this).find('.addtime').text(),
			aid : $(this).attr('data-id'),
			tid : $(this).attr('data-tid'),
			body : $(this).find('.content').html(),
			bounty : $(this).find('.bounty').text(),
			uid : $(this).attr('data-uid')
		});
		bbs_inf.addEventListener('loaded', function (){
			bbs_inf.show('pop-in' , 300); 
			plus.nativeUI.closeWaiting();
		})	
	})
	
}






window.addEventListener('update_number',function(event){
    var id = event.detail.id; 
    $.ajax({  
		type:"post", 
		url:apiRoot + '/Home/Article/reply_number/',
		data:{
			aid : id  
		},
		dataType : 'json', 
		success : function(data){ 
			$('.number' + id).text(data); 
		},
		error : function(e){
			toast('连接超时,请检查网络');
			plus.nativeUI.closeWaiting();
		}
	});
});


function addPost(){
	info();
}

//window.addEventListener('add_post',function(event){
//	info();
////	html(0 , event.detail.id ,30 , event.detail.avatar , event.detail.nickname , '刚刚' , event.detail.title , event.detail.body , 0 , 0)
////	html(2 , event.detail.id ,30 , event.detail.avatar , event.detail.nickname , '刚刚' , event.detail.title , event.detail.body , 0 , 0)
//});


